<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="文件名称" prop="fileOriginName">
        <el-input
          v-model="queryParams.fileOriginName"
          placeholder="请输入文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件类型" prop="fileSuffix">
        <el-input
          v-model="queryParams.fileSuffix"
          placeholder="例如txt、gif"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内容关键字" prop="fileObjectName">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入搜索内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="课件类型" prop="fileObjectName">
        <el-select v-model="queryParams.classType" placeholder="请选择课件类型">
          <el-option
            v-for="dict in dict.type.class_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    </el-form>

    <el-table v-loading="loading" :data="fileInfoList">
      <el-table-column v-if="false" type="selection" width="55" align="center"/>
      <!-- <el-table-column label="文件主键id" align="center" prop="fileId" /> -->
      <el-table-column label="文件编号" align="center" prop="fileId" width="80"/>
      <el-table-column label="文件名称" align="center" prop="fileOriginName"/>
      <el-table-column label="文件类型" align="center" prop="fileSuffix" width="80"/>
      <el-table-column label="内容关键字" align="center" prop="keyword"/>

      <el-table-column label="文件大小" align="center" prop="fileSizeInfo" width="100"/>
      <el-table-column label="存储文件名称" v-if="false" align="center" prop="fileObjectName"/>
      <el-table-column label="课件类型" align="center" width="80" prop="delFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.class_type" :value="scope.row.classType"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="存储路径" align="center" prop="filePath" /> -->
      <el-table-column label="上传者" align="center" prop="createBy" width="80"/>
      <el-table-column label="上传时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewPic(scope.row)"
          >添加学生
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="文件预览" :visible.sync="open" width="500px" append-to-body>
      <!-- 用 iframe 嵌入 PDF -->
      <iframe
        :src="activeFile"
        width="100%"
        height="600px"
        frameborder="0"
      ></iframe>
    </el-dialog>
  </div>
</template>

<script>
import {
  listClassFile,
  getFileInfo,
  delFileInfo,
  addFileInfo,
  updateFileInfo,
  listClassFileAlready
} from "@/api/cms/fileInfo";
import {getToken} from "@/utils/auth";
import image from '../../../assets/images/file.png'
import store from '@/store';
import studentAvatar from "@/views/system/user/profile/studentAvatar.vue";

export default {
  name: "FileInfo",
  dicts: ['sys_yes_no', 'class_type'],
  components: {studentAvatar},
  data() {
    return {
      // 遮罩层
      loading: true,
      activeFile: null, // 当前预览文件
      // 选中数组
      ids: [],
      fileNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件管理表格数据
      fileInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上传的文件列表
        fileList: []
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileOriginName: null,
        fileSuffix: null,
        keyword: null,
        fileSizeInfo: null,
        fileObjectName: null,
        filePath: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文件管理列表 */
    getList() {
      this.loading = true;
      // console.log(this.queryParams);
      listClassFileAlready(this.queryParams).then(response => {
        // console.log(response);
        this.fileInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fileId: null,
        fileOriginName: null,
        fileSuffix: null,
        fileSizeInfo: null,
        fileObjectName: null,
        filePath: null,
        keyword: null,
        delFlag: null,
        createBy: store.getters.name,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    viewPic(row) {
      this.open = true;
      this.activeFile = 'http://101.43.194.26:84/prod-api' + row.filePath
      console.log(this.activeFile);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.keyword = undefined
      this.queryParams.classType = undefined
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
<style scoped>

.text-preview {
  flex: 1;
  margin-top: 10px;
  border: 1px dashed #ddd;
  border-radius: 6px;
  padding: 15px;
  overflow-y: auto;
  background-color: #f9f9f9;
}

.text-preview pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  margin: 0;
  color: #333;
  font-family: monospace;
  font-size: 14px;
}

.pdf-preview {
  flex: 1;
  margin-top: 10px;
  border: 1px dashed #ddd;
  border-radius: 6px;
  overflow: hidden;
}

.pdf-frame {
  width: 100%;
  height: 100%;
  border: none;
}
</style>
